package com.hiv.mapper;

import com.hiv.common.param.DataPreviewStatisticsParam;
import com.hiv.common.param.page.PageQueryHospitalParam;
import com.hiv.mapper.domain.Hospital;
import java.util.List;
import java.util.Set;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 机构/医院/检验所Mapper接口
 * 
 * @author zhanghong
 * @date 2022-09-18
 */
@Mapper
@Repository
public interface HospitalMapper {
    Hospital selectById(Long id);
    List<Hospital> selectList(Hospital hospital);
    int insert(Hospital hospital);
    int batchInsert(List<Hospital> hospitalList);
    int update(Hospital hospital);
    int deleteById(Long id);
    int deleteByIds(String[] ids);

    @Select({"<script>",
            "   select count(1) from hospital",
            "   <where>",
            "       deleted=0",
            "       <if test=\"param.parentId!=null\">and parent_id=#{param.parentId}</if>",
            "       <if test=\"param.type!=null\">and type=#{param.type}</if>",
            "       <if test=\"param.name!=null and param.name!=''\">and name like concat('%',#{param.name},'%')</if>",
            "   </where>",
            "</script>"})
    long pageCount(@Param("param") PageQueryHospitalParam param);
    @Select({"<script>",
            "   select * from hospital",
            "   <where>",
            "       deleted=0",
            "       <if test=\"param.parentId!=null\">and parent_id=#{param.parentId}</if>",
            "       <if test=\"param.type!=null\">and type=#{param.type}</if>",
            "       <if test=\"param.name!=null and param.name!=''\">and name like concat('%',#{param.name},'%')</if>",
            "   </where>",
            "   order by create_time desc limit #{limit} offset #{offset}",
            "</script>"})
    List<Hospital> pageQuery(@Param("param") PageQueryHospitalParam param, @Param("limit") int limit, @Param("offset") long offset);

    @Select("select * from hospital where parent_id=#{parentId}")
    List<Hospital> listByParentId(Long parentId);

    @Select("select * from hospital where status=1 and parent_id=#{parentId}")
    List<Hospital> listTreeByParentId(Long parentId);

    @Update("update hospital set status=#{status} where parent_id=#{parentId}")
    Integer updateStatusByParentId(@Param("status") Integer status, @Param("parentId") Long parentId);

    @Select("select * from hospital where parent_id=#{parentId} and status=1")
    List<Hospital> queryEnableBroByParentId(Long parentId);

    @Update("update hospital set status=#{status} where id=#{id}")
    Integer updateStatusById(@Param("status") Integer status, @Param("id") Long id);

    @Select({"<script>",
            "   select * from hospital",
            "   <where>",
            "       deleted=0",
            "       <if test=\"hospitalIdList != null and hospitalIdList.size() != 0\">",
            "           and id in",
            "           <foreach collection=\"hospitalIdList\" item=\"item\" open=\"(\" separator=\",\" close=\")\"> ",
            "               #{item} ",
            "           </foreach> ",
            "       </if> ",
            "   </where>",
            "</script>"})
    List<Hospital> queryByIds(@Param("hospitalIdList") Set<Long> hospitalIdList);

    @Select({"<script>",
            "   select count(1) from hospital",
            "   <where>",
            "       deleted=0",
            "       <if test=\"type != null\">and type=#{type}</if> ",
            "   </where>",
            "</script>"})
    Integer hospitalCountByType(@Param("type") Integer type);
}
